package com.chinasoft.goodsManager.servlet;

import com.chinasoft.goodsManager.doamain.Order;
import com.chinasoft.goodsManager.service.OrderService;
import com.chinasoft.goodsManager.service.impl.OrderServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

@WebServlet("/orderAdd")
public class OrderAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Order order = new Order();
        String contextPath = req.getContextPath();
        order.setPname(req.getParameter("aPname"));
        System.out.println(req.getParameter("single-date-picker"));
        try {
            System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse(req.getParameter("single-date-picker")));
            order.setOdate(new SimpleDateFormat("MM/dd/yyyy").parse(req.getParameter("single-date-picker")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setAddress(req.getParameter("aaddress"));
        order.setCategory(req.getParameter("aleibie"));
        order.setPrice(Double.parseDouble(req.getParameter("aprice")));
        order.setNum(Integer.parseInt(req.getParameter("anum")));
        order.setOprice(Double.parseDouble(req.getParameter("aoprice")));
        System.out.println(order);
        OrderService service = new OrderServiceImpl();
        boolean flag = service.add(order);
        resp.setHeader("Content-Type", "application/json;charset=UTF-8");
        if (flag) {
            resp.getWriter().write("1");
        } else
            resp.getWriter().write("0");
    }
}
